package org.basic.mail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.basic.common.core.utils.MapstructUtils;
import org.basic.common.mybatis.core.page.TableDataInfo;
import org.basic.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.basic.mail.domain.SysMailAccount;
import org.basic.mail.domain.bo.SysMailAccountBo;
import org.basic.mail.domain.vo.SysMailAccountVo;
import org.basic.mail.domain.vo.SysSimpleMailAccountVo;
import org.basic.mail.mapper.SysMailAccountMapper;
import org.basic.mail.service.ISysMailAccountService;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 邮箱账号Service业务层处理
 *
 * @author Shao Hua
 * @date 2023-07-27
 */
@RequiredArgsConstructor
@Service
public class SysMailAccountServiceImpl implements ISysMailAccountService {

    private final SysMailAccountMapper baseMapper;

    /**
     * 查询邮箱账号
     */
    @Override
    public SysMailAccountVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询账号邮箱带密码
     * @param id
     * @return
     */
    @Override
    public SysMailAccount queryByIdToSend(Long id) {
        return baseMapper.selectById(id);
    }
    /**
     * 查询邮箱账号列表
     */
    @Override
    public TableDataInfo<SysMailAccountVo> queryPageList(SysMailAccountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysMailAccount> lqw = buildQueryWrapper(bo);
        Page<SysMailAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询邮箱账号列表
     */
    @Override
    public List<SysMailAccountVo> queryList(SysMailAccountBo bo) {
        LambdaQueryWrapper<SysMailAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysMailAccount> buildQueryWrapper(SysMailAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysMailAccount> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMail()), SysMailAccount::getMail, bo.getMail());
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), SysMailAccount::getUsername, bo.getUsername());
        return lqw;
    }

    /**
     * 新增邮箱账号
     */
    @Override
    public Boolean insertByBo(SysMailAccountBo bo) {
        SysMailAccount add = MapstructUtils.convert(bo, SysMailAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮箱账号
     */
    @Override
    public Boolean updateByBo(SysMailAccountBo bo) {
        SysMailAccount update = MapstructUtils.convert(bo, SysMailAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysMailAccount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邮箱账号
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取邮箱数组信息
     * @return
     */
    @Override
    public List<SysSimpleMailAccountVo> getSimpleMailAccountList() {
        LambdaQueryWrapper<SysMailAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysMailAccount::getId, SysMailAccount::getMail);
        List<SysMailAccount> sysMailAccounts = baseMapper.selectList(queryWrapper);
        List<SysSimpleMailAccountVo> simpleMailAccountVoList = new ArrayList<>();
        for (SysMailAccount sysMailAccount : sysMailAccounts) {
            SysSimpleMailAccountVo sysSimpleMailAccountVo = new SysSimpleMailAccountVo();
            BeanUtil.copyProperties(sysMailAccount, sysSimpleMailAccountVo);
            simpleMailAccountVoList.add(sysSimpleMailAccountVo);
        }
        return simpleMailAccountVoList;
    }
}
